#include <string.h>
#include "sk6812_spi.h"

uint8_t sk6812_buffer[sk6812_BUFFER_SIZE];

#define sk6812_FILL_BUFFER(COLOR) \
    for( uint8_t mask = 0x80; mask; mask >>= 1 ) { \
        if( COLOR & mask ) { *ptr++ = 0xfc; } \
        else { *ptr++ = 0x80; }}


void sk6812_init(void) {
    memset(sk6812_buffer, 0, sk6812_BUFFER_SIZE);
    sk6812_send_spi();
}

void sk6812_send_spi(void)
{
    /* 使用HAL库的阻塞传输方式发送整个缓冲区 */
    HAL_SPI_Transmit(&sk6812_SPI_HANDLE, sk6812_buffer, sk6812_BUFFER_SIZE, HAL_MAX_DELAY);
}

void sk6812_pixel(uint16_t led_no, uint8_t r, uint8_t g, uint8_t b, uint8_t w) {
    uint8_t * ptr = &sk6812_buffer[32 * led_no];
    sk6812_FILL_BUFFER(g);
    sk6812_FILL_BUFFER(r);
    sk6812_FILL_BUFFER(b);
    sk6812_FILL_BUFFER(w);
}

void sk6812_pixel_all(uint8_t r, uint8_t g, uint8_t b, uint8_t w) {
    uint8_t * ptr = sk6812_buffer;
    for( uint16_t i = 0; i < sk6812_NUM_LEDS; ++i) {
        sk6812_FILL_BUFFER(g);
        sk6812_FILL_BUFFER(r);
        sk6812_FILL_BUFFER(b);
        sk6812_FILL_BUFFER(w);
    }

}



